package com.candy.candyuser.model.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.candy.candycommon.utils.ResultBean;
import com.candy.candyproductservice.dto.ProductDTO;
import com.candy.candyproductservice.service.ProductService;
import com.candy.candyuser.model.entity.User;
import com.candy.candyuser.model.mapper.UserMapper;
import com.candy.candyuser.model.service.UserService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author suYanLong
 * @date 2023年02月10日 16:36
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final ProductService productService;

    // @PostConstruct
    public void test() {
        User userInsert = new User();
        userInsert.setId(3l);
        userInsert.setName("suyanlong");
        userInsert.setCardNo("130131199312161218");
        userInsert.setPhone("15930154499");
//        this.save(userInsert);
//        this.updateById(userInsert);
//        RootContext.getXID()
        List<User> list = baseMapper.getListByCardNoAndPhone("130131199312161218", "15930154459");
        log.info(list.size() + "");
//        User user = this.getById(3);
//        log.info(user.toString());
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public ResultBean addUser() {
        ProductDTO productDTO = new ProductDTO();
        productDTO.setId(1l);
        productDTO.setSubNum(1);
        ResultBean resultBean = productService.subProduct(productDTO);
        log.info("扣减库存结果：{}", resultBean);

        User userInsert = new User();
        userInsert.setId(3l);
        userInsert.setName("suyanlong");
        userInsert.setCardNo("130131199312161218");
        userInsert.setPhone("15930154499");
        baseMapper.insert(userInsert);

//        int i = 1 / 0;
        return ResultBean.ok();
    }

    @Override
    @Transactional
    public ResultBean addUserTcc() {
        log.info("addUserTcc=========================");
        User userInsert = new User();
        userInsert.setId(3l);
        userInsert.setName("suyanlong");
        userInsert.setCardNo("130131199312161218");
        userInsert.setPhone("15930154499");
        baseMapper.insert(userInsert);
        return ResultBean.ok();
    }

    @Override
    public Boolean addUserTccCommit(BusinessActionContext businessActionContext) {
        log.info("addUserTccCommit=========================");
        return true;
    }

    @Override
    public Boolean addUserTccRollback(BusinessActionContext businessActionContext) {
        log.info("addUserTccRollback=========================");
        return true;
    }

}
